<template>
  <div>
    <!-- 头部标签 -->
    <van-nav-bar fixed>
      <template #left>
        <!-- <img class="logo" src="../../assets/toutiao_logo.png" alt="" /> -->
        <img class="logo" :src="imgObj"/>
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" color="#fff" @click="$router.push('/search')"/>
      </template>
    </van-nav-bar>

    <!-- 频道部分 -->
    <!-- sticky 粘性定位   offset-top距离 -->
<van-tabs v-model="channelId" animated  sticky offset-top="1.226667rem">
  <van-tab  v-for="item in channelList" :key="item.id"  :title="item.name">

    <article-list :cid='channelId'/>

  </van-tab>

</van-tabs>
<!-- 编辑频道图标+ -->
<van-icon name="plus" size="0.37333334rem" class="moreChannels" @click="show=true"/>
    <van-popup v-model="show" class="edit_wrap" @closed="onPopupClosed">
      <!-- 弹出层的主体区域 -->
      <!-- @是传事件 -->
      <ChannelEdit
      ref="channelEdit"
      :userChannelList='channelList'
      @close="show = false"
      @addChannel="addChannelFn"
      @removeChannel="removeChannelFn"
      @changeChannel="changeChannelFn"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import ChannelEdit from './ChannelEdit.vue'
import { userChannelAPI, updateChannelAPI } from '@/api'
import logoPng from '../../assets/toutiao_logo.png'
import ArticleList from './ArticleList.vue'
export default {
  components: { ArticleList, ChannelEdit },
  name: 'Home',
  data () {
    return {
      show: false,
      imgObj: logoPng,
      channelId: 0,
      // 频道数据
      channelList: []
    }
  },
  async created () {
    // 获取频道数据
    const res = await userChannelAPI()
    console.log(res)
    const { channels } = res.data.data
    this.channelList = channels
  },
  methods: {
    async addChannelFn (obj) {
      this.channelList.push(obj)
      this.channelFn()
    },
    removeChannelFn (val) {
      const res = this.channelList.findIndex(item => {
        return item.id === val.id
      })
      this.channelList.splice(res, 1)
      this.channelFn()
    },
    async channelFn () {
      const list = this.channelList.filter(item => {
        return item.id !== 0
      })
      const resArr = list.map((item, index) => {
        const newObj = { ...item }
        newObj.req = index + 1
        delete newObj.name
        return newObj
      })
      await updateChannelAPI({
        channels: resArr
      })
    },
    onPopupClosed () {
      console.log(this.$refs)
      this.$refs.channelEdit.isEdit = true
    },
    changeChannelFn (val) {
      this.channelId = val.id
      console.log(val)
      // console.log(this.channelId)
    }
  }

}
</script>
<style scoped lang="less">
.logo {
    width: 100px;
    height: 30px;
}
/* tab内容距离tab导航的距离 */
/deep/ .van-tabs__content{
    padding-top: 44px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
.edit_wrap{
  width: 100%;
  height: 100%;
}
</style>
